package com.lpx.jellyanim;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;

import com.lpx.jellyanim.circularReveal.animation.ViewAnimationUtils;

import static android.view.View.LAYER_TYPE_HARDWARE;

public class MainActivity extends AppCompatActivity {
    private LinearLayout popLayout;
    private Button starBtn;

    private LinearLayout popImageLayout;
    private Button starBtnImage;
    private LinearLayout oneImgLayout,twoImgLayout,threeImgLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        popLayout = findViewById(R.id.ll_pop);
        popLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popAnim(false);
            }
        });
        starBtn = findViewById(R.id.btn_background);
        popImageLayout = findViewById(R.id.ll_pop_bg_image);
        popImageLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popImageAnim(false);
            }
        });
        starBtnImage = findViewById(R.id.btn_bg_image);
        oneImgLayout = findViewById(R.id.ll_one);
        twoImgLayout = findViewById(R.id.ll_two);
        threeImgLayout = findViewById(R.id.ll_three);
    }

    public void StarPopWindow(View view) {
            popAnim(true);
    }

    private void popAnim(final boolean b) {
        popLayout.clearAnimation();
        if (b){
            popLayout.setVisibility(View.VISIBLE);
        }
        int cx = (starBtn.getLeft() + starBtn.getRight())/ 2;
        int cy = (starBtn.getTop() + starBtn.getBottom()) /2;

        int dx = Math.max(cx,popLayout.getWidth() - cx);
        int dy = Math.max(cy,popLayout.getHeight() - cy);
        float finalRadius = (float) Math.hypot(dx,dy);
        Animator circular = ViewAnimationUtils.createCircularReveal(popLayout, cx, cy, b ? 0 : finalRadius,
                b ? finalRadius : 0, LAYER_TYPE_HARDWARE);
        circular.setInterpolator(new FastOutSlowInInterpolator());
        circular.setDuration(500);
        circular.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (b) {

                } else {
                    popLayout.setVisibility(View.GONE);
                }
            }
        });
        circular.start();
    }

    public void StarPopAndImage(View view) {
            popImageAnim(true);
    }

    private void popImageAnim(final boolean b) {
        popImageLayout.clearAnimation();
        oneImgLayout.clearAnimation();
        twoImgLayout.clearAnimation();
        threeImgLayout.clearAnimation();
        if (b){
            popImageLayout.setVisibility(View.VISIBLE);
            Animation oneAnim = AnimationUtils.loadAnimation(this,R.anim.icon_in);
            Animation twoAnim = AnimationUtils.loadAnimation(this,R.anim.icon_in);
            Animation threeAnim = AnimationUtils.loadAnimation(this,R.anim.icon_in);
            oneAnim.setStartOffset(100);
            oneImgLayout.startAnimation(oneAnim);
            twoAnim.setStartOffset(200);
            twoImgLayout.startAnimation(twoAnim);
            threeAnim.setStartOffset(300);
            threeImgLayout.startAnimation(threeAnim);
        }
        int cx = (starBtnImage.getLeft() + starBtnImage.getRight())/2;
        int cy = (starBtnImage.getTop() + starBtnImage.getBottom())/2;
        int dx = Math.max(cx,popImageLayout.getWidth() - cx);
        int dy = Math.max(cy,popImageLayout.getHeight() - cy);
        float finalRadius = (float) Math.hypot(dx,dy);
        Animator circular = ViewAnimationUtils.createCircularReveal(popImageLayout,cx,cy,b?0:finalRadius,
                b ? finalRadius :0,LAYER_TYPE_HARDWARE);
        circular.setInterpolator(new FastOutSlowInInterpolator());
        circular.setDuration(500);
        circular.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (b) {

                } else {
                    popImageLayout.setVisibility(View.GONE);
                }
            }
        });
        circular.start();
    }
}
